package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(logestMountainLest([]int{2, 1, 4, 7, 3, 2, 5}))
	//Left							  0  1  2  3  0  0  1
	//right							  1  0  0  2  1  0  0
}

func longestMountain(arr []int) int {
	n := len(arr)
	left := make([]int, n)
	for i := 1; i < n; i++ {
		if arr[i-1] < arr[i] {
			left[i] = left[i-1] + 1
		}
	}
	right := make([]int, n)
	for i := n - 2; i >= 0; i-- {
		if arr[i+1] < arr[i] {
			right[i] = right[i+1] + 1
		}
	}
	ans := 0
	for i, l := range left {
		r := right[i]
		if l > 0 && r > 0 && l+r+1 > ans {
			ans = l + r + 1
		}
	}
	return ans
}

func logestMountainLest(arr []int) int {
	ans := 0
	tmp := 0
	for i := 1; i < len(arr); i++ {
		if arr[i] > arr[i-1] && tmp >= 0 {
			tmp++
		} else if arr[i] < arr[i-1] && tmp <= 0 {
			tmp--
		} else if arr[i] < arr[i-1] && tmp > 0 {
			tmp = -tmp - 1
		} else if arr[i] > arr[i-1] && tmp < 0 {
			tmp = 1
		} else {
			tmp = 0
		}
		if int(math.Abs(float64(tmp)))+1 > ans {
			ans = int(math.Abs(float64(tmp))) + 1
		}
	}
	return ans
}
